import config from "@/config/config.js"
import storage from "@/uni-sub-base/utils/storage.js"
const appUrl = {
    getBaseUrls() {
        let urlObj
        //判断环境
        //TODO 待优化 改成不每次都取 storage
        let appBaseUrl = storage.getCache("appBaseUrl")
        let appService = storage.getCache("appService")
        console.log("appService", appService)
        if (!appService) {
            //格式化防止深拷贝
            urlObj =
                config.useUrl.type == 1
                    ? JSON.parse(JSON.stringify(config.releaseUrl))
                    : JSON.parse(JSON.stringify(config.testUrl))
        } else {
            //格式化防止深拷贝
            urlObj =
                appService == 1
                    ? JSON.parse(JSON.stringify(config.releaseUrl))
                    : JSON.parse(JSON.stringify(config.testUrl))
        }
        if (appBaseUrl) {
            urlObj.baseUrl = appBaseUrl
        }
        let obj = {
            baseUrl: urlObj.baseUrl,
            baseUrlTms: urlObj.baseUrl,
            baseUrlGuide: urlObj.baseUrl + "guide/",
            baseUrlSupplier: urlObj.baseUrl + "supplier/",
            basePublicUrl: urlObj.baseUrl,
            ssoUrl: urlObj.ssoUrl,
        }
        config.baseUrl = urlObj.baseUrl
        config.basePublicUrl = obj.basePublicUrl
        config.ssoUrl = obj.ssoUrl

        return obj
    },
    setBaseUrls(baseUrl) {
        storage.setCache("appBaseUrl", baseUrl)
    },
    getShopUrls() {
        let urlObj
        //判断环境
        //TODO 待优化 改成不每次都取 storage
        let appBaseUrl = storage.getCache("appBaseUrl")
        let appService = storage.getCache("appService")
        if (!appService) {
            urlObj = config.useUrl.type == 1 ? config.releaseUrl : config.testUrl
        } else {
            urlObj = appService == 1 ? config.releaseUrl : config.testUrl
        }
        if (appBaseUrl) {
            urlObj.baseUrl = appBaseUrl
        }
        urlObj = JSON.parse(JSON.stringify(urlObj))
        let obj = {
            baseUrl: urlObj.baseUrl,
            baseUrlTms: urlObj.baseUrl,
            baseUrlGuide: urlObj.baseUrl + "guide/",
            baseUrlSupplier: urlObj.baseUrl + "supplier/",
            basePublicUrl: urlObj.baseUrl,
            ssoUrl: urlObj.ssoUrl,
        }
        config.baseUrl = urlObj.baseUrl
        config.basePublicUrl = obj.basePublicUrl
        config.ssoUrl = obj.ssoUrl
    },
}
module.exports = {
    getBaseUrls: appUrl.getBaseUrls,
    setBaseUrls: appUrl.setBaseUrls,
    getShopUrls: appUrl.getShopUrls,
}
